package com.liu.spit.controller;

import com.liu.common.entity.PageResult;
import com.liu.common.entity.Result;
import com.liu.common.status.CodeEnum;
import com.liu.common.status.RedisKeyEnum;
import com.liu.spit.pojo.Spit;
import com.liu.spit.service.SpitService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

@RestController
@CrossOrigin
@RequestMapping("/spit")
public class SpitController {

    @Autowired
    private SpitService spitService;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 查询所有的
     * @return
     */
    @RequestMapping(method = RequestMethod.GET)
    public Result findAll(){

        return  new Result(true, CodeEnum.OK.getCode(),"查询成功！",spitService.findAll());
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public Result findById(@PathVariable String id){

        return  new Result(true, CodeEnum.OK.getCode(),"查询成功！",spitService.findById(id));
    }

    @RequestMapping(method = RequestMethod.POST)
    public Result add(@RequestBody  Spit spit){
        spitService.add(spit);
        return   new Result(true, CodeEnum.OK.getCode(),"保存成功！");
    }
    @RequestMapping(value ="/{id}" ,method = RequestMethod.PUT)
    public Result update(@RequestBody  Spit spit,@PathVariable String id ){
        spit.set_id(id);
        spitService.update(spit);
        return   new Result(true, CodeEnum.OK.getCode(),"更新成功！");
    }

    @RequestMapping(value = "/{id}",method = RequestMethod.DELETE)
    public Result deleteById(@PathVariable String id){
        spitService.deleteById(id);
        return  new Result(true, CodeEnum.OK.getCode(),"删除成功！");

    }


    /**
     * 根据上级ID查询吐槽数据
     * @param parentid
     * @param page
     * @param size
     * @return
     */
    @RequestMapping(value = "comment/{parentid}/{page}/{size}",method = RequestMethod.GET)
    public Result findByParentId(@PathVariable String parentid,@PathVariable int page, @PathVariable int size){

       Page<Spit> spitPage = spitService.findByParentId(parentid,page,size);

       return  new Result(true,CodeEnum.OK.getCode(),"查询成功",new PageResult<Spit>(spitPage.getTotalElements(),spitPage.getContent()));


    }

   @RequestMapping(value = "thumbup/{spitId}",method = RequestMethod.PUT)
    public Result thumbup(@PathVariable  String spitId){
//    userId 使用来模拟用户登录状态。
        String userId = "123";
//  这里用缓存，来记录点赞状态，当用户点赞后，用户对该吐槽不能二次点赞
       if (redisTemplate.opsForValue().get(RedisKeyEnum.APIT_THUMBUP_KEY.getKey()+spitId+userId)!=null){
           return  new Result(false,CodeEnum.REPE_ERROR.getCode(),"您已经点赞，不能重复点赞");

       }
       spitService.thumbup(spitId);
//       用户对该吐槽条目进行点赞后，记得修改点赞的标识位
       redisTemplate.opsForValue().set(RedisKeyEnum.APIT_THUMBUP_KEY.getKey()+spitId+userId,1);
       return new Result(true,CodeEnum.OK.getCode(),"点赞成功");
    }




}
